/**
 * Copyright (C) 2009 Bruno Dumas (bruno.dumas -at- unifr.ch), DIVA group, University of Fribourg, Switzerland
 * 
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */

//SMUIML v0.4

// to be renamed? :)

package diuf.diva.hephaistk.xml.smuiml;

import java.util.Vector;

import it.unina.lab.costants.Constants;
import diuf.diva.hephaistk.xml.Attribute;
import diuf.diva.hephaistk.xml.HephaisTKParsingException;
import diuf.diva.hephaistk.xml.XmlGeneric;

public class Trigger extends Smuiml_Generic {
	/**
	 *
	 */
	private static final long serialVersionUID = 5002043803933345634L;

	public final static String NAME = "trigger";

	private Attribute attName = null;
	private Attribute attModality = null;
	private Vector<Variable> variables = null;
	private Source source = null;

	public Trigger(){
		super();
		setName(NAME);
		variables = new Vector<Variable>();
	}

	
	public void addVariable(Variable variable){
		variables.add(variable);
		elements.add(variable);
	}
	
	
	public Variable getVariable(int index){
		return variables.get(index);
	}

	public Vector<Variable> getVariables(){
		return variables;
	}

	public int variablesSize(){
		return variables.size();
	}
	
	
	public Attribute getAttModality() {
		return attModality;
	}





	public void setAttModality(String attModality) {
		this.attModality = new Attribute("modality", attModality);
	}





	/**
	 * @return the attName
	 */
	public Attribute getAttName() {
		return attName;
	}

	/**
	 * @param attName the attName to set
	 */
	public void setAttName(String attName) {
		this.attName = new Attribute("name", attName);
	}

	/**
	 * @return the source
	 */
	public Source getSource() {
		return source;
	}

	/**
	 * @param source the source to set
	 */
	public void setSource(Source source) {
		this.source = source;
	}

	public void addElement(XmlGeneric element) throws HephaisTKParsingException {
		if(element instanceof Variable){
			addVariable((Variable)element);
		}else{
			throw new HephaisTKParsingException(getNamespace()+"."+NAME+" -- unexpected element: "+element.getName()+"only <"+Source.NAME+"> allowed");
		}
	}

	@Override
	public void addAttribute(Attribute attribute) throws HephaisTKParsingException{
		super.addAttribute(attribute);
		String attName = attribute.getName();
		if (attName.indexOf(':') != -1) {
			attName = attName.substring(attName.indexOf(':')+1);
		}
		addAttribute(attName, attribute.getValue());
	}

	public void addAttribute(String name, String value) throws HephaisTKParsingException{
		if(name.equalsIgnoreCase("name")){
			setAttName(value);
		}else if(name.equalsIgnoreCase("modality")){
				setAttModality(value);
			} 
		else {	
			throw new HephaisTKParsingException(getNamespace()+"."+NAME+" -- unexpected attribute: "+name);
		}
	}

	public String toString(int indent) {
		StringBuilder ret = new StringBuilder();
		for(int i = 0; i < indent; i++){
			ret.append(' ');
		}
		ret.append("<"+NAME+(attName != null? " name=\""+attName.getValue()+"\"" : "")+">\n");
		if(source != null){
			ret.append(source.toString(indent+Constants.INDENT_PREFIX));
		}
		for(int i = 0; i < indent; i++){
			ret.append(' ');
		}
		ret.append("</"+NAME+">\n");
		return ret.toString();
	}
}
